extends Control

# 引用
var player: Player
var data_manager: DataManager

# 战斗模式
var battle_mode: bool = false
var battle_system = null

# 设置战斗模式
func set_battle_mode(is_battle_mode: bool, p_battle_system = null) -> void:
	battle_mode = is_battle_mode
	battle_system = p_battle_system
	
	# 更新UI以反映战斗模式状态
	update_ui()

# 当前选中的物品
var selected_item_index: int = -1

# 空装备槽位点击处理函数
func _on_empty_equipment_slot_pressed(slot_index: int) -> void:
	# 如果有选中的物品，尝试装备到这个槽位
	if selected_item_index >= 0 and selected_item_index < player.inventory.size():
		var item = player.inventory[selected_item_index]
		
		# 检查物品是否可装备且槽位匹配
		if item.can_equip and item.equip_slot == slot_index:
			# 装备物品
			player.equip_item(selected_item_index)
			
			# 更新界面
			update_ui()
			
			# 重置选中的物品
			selected_item_index = -1

# 节点引用
@onready var equipment_container = $MarginContainer/VBoxContainer/Content/EquipmentSlots/GridContainer
@onready var item_info_name = $MarginContainer/VBoxContainer/Content/ItemDetails/Panel/VBoxContainer/NameLabel
@onready var item_info_type = $MarginContainer/VBoxContainer/Content/ItemDetails/Panel/VBoxContainer/TypeLabel
@onready var item_info_description = $MarginContainer/VBoxContainer/Content/ItemDetails/Panel/VBoxContainer/DescriptionLabel
@onready var item_info_stats = $MarginContainer/VBoxContainer/Content/ItemDetails/Panel/VBoxContainer/StatsContainer
@onready var equip_button = $MarginContainer/VBoxContainer/Content/ItemDetails/Panel/VBoxContainer/ButtonsContainer/EquipButton
@onready var unequip_button = $MarginContainer/VBoxContainer/Content/ItemDetails/Panel/VBoxContainer/ButtonsContainer/UnequipButton
@onready var inventory_container = $MarginContainer/VBoxContainer/Content/Inventory/ScrollContainer/InventoryContainer
@onready var use_button = $MarginContainer/VBoxContainer/Content/ItemDetails/Panel/VBoxContainer/ButtonsContainer/UseButton
@onready var drop_button = $MarginContainer/VBoxContainer/Content/ItemDetails/Panel/VBoxContainer/ButtonsContainer/DropButton
@onready var player_stats_container = null # 在当前场景中不存在
@onready var spirit_stone_label = $MarginContainer/VBoxContainer/TitleBar/SpiritStoneContainer/SpiritStoneLabel

# 初始化节点
func _ready() -> void:
	# 等待节点树准备就绪
	await ready
	
	# 检查节点引用是否正确
	if not equipment_container:
		push_error("装备容器节点未找到")
	
	# 连接按钮信号
	if equip_button:
		if not equip_button.pressed.is_connected(_on_equip_button_pressed):
			equip_button.pressed.connect(_on_equip_button_pressed)
	
	if unequip_button:
		if not unequip_button.pressed.is_connected(_on_unequip_button_pressed):
			unequip_button.pressed.connect(_on_unequip_button_pressed)
	
	if use_button:
		if not use_button.pressed.is_connected(_on_use_button_pressed):
			use_button.pressed.connect(_on_use_button_pressed)
	
	if drop_button:
		if not drop_button.pressed.is_connected(_on_drop_button_pressed):
			drop_button.pressed.connect(_on_drop_button_pressed)

# 初始化
func initialize(p_player: Player, p_data_manager: DataManager) -> void:
	player = p_player
	data_manager = p_data_manager
	
	# 断开旧的信号连接
	if player:
		if player.inventory_changed.is_connected(_on_inventory_changed):
			player.inventory_changed.disconnect(_on_inventory_changed)
		if player.equipment_changed.is_connected(_on_equipment_changed):
			player.equipment_changed.disconnect(_on_equipment_changed)
	
	# 断开旧的信号连接
	if player:
		if player.inventory_changed.is_connected(_on_inventory_changed):
			player.inventory_changed.disconnect(_on_inventory_changed)
		if player.equipment_changed.is_connected(_on_equipment_changed):
			player.equipment_changed.disconnect(_on_equipment_changed)
	
	# 连接新的信号
	player.inventory_changed.connect(_on_inventory_changed)
	player.equipment_changed.connect(_on_equipment_changed)
	
	# 更新界面
	update_ui()

# 背包变化信号处理
func _on_inventory_changed() -> void:
	update_ui()

# 装备变化信号处理
func _on_equipment_changed(_slot: int, _item) -> void:
	update_ui()

# 显示界面
func show_ui() -> void:
	visible = true
	
	# 确保获取最新的玩家数据
	if player:
		# 重新连接信号，确保不会重复连接
		if not player.inventory_changed.is_connected(_on_inventory_changed):
			player.inventory_changed.connect(_on_inventory_changed)
		if not player.equipment_changed.is_connected(_on_equipment_changed):
			player.equipment_changed.connect(_on_equipment_changed)
	
	# 刷新界面
	update_ui()

# 隐藏界面
func hide_ui() -> void:
	visible = false

# 更新界面
func update_ui() -> void:
	# 检查玩家引用
	if not player:
		push_error("玩家引用未设置")
		return
	
	# 更新灵石显示
	if spirit_stone_label:
		spirit_stone_label.text = str(player.spirit_stones)
	else:
		push_error("灵石标签节点未找到")
	
	# 更新装备槽位
	if equipment_container:
		update_equipment_slots()
	else:
		push_error("装备容器节点未找到")
	
	# 更新背包内容
	if inventory_container:
		update_inventory()
	else:
		push_error("背包容器节点未找到")
	
	# 更新物品信息
	if item_info_name and item_info_type and item_info_description and item_info_stats:
		update_item_info()
	else:
		push_error("物品信息节点未找到")
	
	# 更新玩家属性 - 如果节点存在
	if player_stats_container:
		update_player_stats()
	else:
		push_warning("玩家属性容器节点未找到，跳过更新玩家属性")

# 更新装备槽位
func update_equipment_slots() -> void:
	# 检查节点引用
	if not equipment_container:
		push_error("装备容器节点未找到")
		return
	
	# 清空装备槽位
	for child in equipment_container.get_children():
		child.queue_free()
	
	# 创建装备槽位
	var slot_names = [
		"武器",
		"头部",
		"身体",
		"手部",
		"脚部",
		"饰品1",
		"饰品2"
	]
	
	for i in range(slot_names.size()):
		# 创建槽位面板
		var slot_panel = Panel.new()
		slot_panel.custom_minimum_size = Vector2(120, 120)
		
		# 设置面板样式
		var panel_style = StyleBoxFlat.new()
		panel_style.bg_color = Color(0.15, 0.15, 0.15, 0.9)
		panel_style.border_width_left = 2
		panel_style.border_width_top = 2
		panel_style.border_width_right = 2
		panel_style.border_width_bottom = 2
		panel_style.border_color = Color(0.5, 0.5, 0.5, 0.8)
		slot_panel.add_theme_stylebox_override("panel", panel_style)
		
		# 创建垂直布局
		var vbox = VBoxContainer.new()
		vbox.size_flags_horizontal = Control.SIZE_EXPAND_FILL
		vbox.size_flags_vertical = Control.SIZE_EXPAND_FILL
		vbox.custom_minimum_size = Vector2(110, 110)
		slot_panel.add_child(vbox)
		
		# 创建槽位名称标签
		var slot_label = Label.new()
		slot_label.text = slot_names[i]
		slot_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
		vbox.add_child(slot_label)
		
		# 检查该槽位是否有装备
		if player.equipment.has(i) and player.equipment[i] != null:
			var item = player.equipment[i]
			
			# 创建物品名称标签
			var item_label = Label.new()
			item_label.text = item.item_name
			item_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
			item_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
			item_label.custom_minimum_size = Vector2(100, 40)
			vbox.add_child(item_label)
			
			# 设置面板边框颜色以反映物品品质
			panel_style.border_color = item.get_quality_color()
			slot_panel.add_theme_stylebox_override("panel", panel_style)
			
			# 创建物品按钮（透明，覆盖整个面板）
			var item_button = Button.new()
			item_button.flat = true
			item_button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
			item_button.size_flags_vertical = Control.SIZE_EXPAND_FILL
			item_button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
			item_button.custom_minimum_size = Vector2(120, 120)
			item_button.anchor_right = 1.0
			item_button.anchor_bottom = 1.0
			item_button.position = Vector2.ZERO
			
			# 连接按钮信号
			item_button.pressed.connect(_on_inventory_item_pressed.bind(i))
			slot_panel.add_child(item_button)
		else:
			# 创建空槽位标签
			var empty_label = Label.new()
			empty_label.text = "未装备"
			empty_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
			empty_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
			empty_label.custom_minimum_size = Vector2(100, 60)
			vbox.add_child(empty_label)
			
			# 创建空槽位按钮
			var empty_button = Button.new()
			empty_button.flat = true
			empty_button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
			empty_button.size_flags_vertical = Control.SIZE_EXPAND_FILL
			empty_button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
			
			# 连接按钮信号
			empty_button.pressed.connect(_on_empty_equipment_slot_pressed.bind(i))
			slot_panel.add_child(empty_button)
		
		# 添加到容器
		equipment_container.add_child(slot_panel)

# 更新背包
# 更新背包
func update_inventory() -> void:
	# 检查节点引用
	if not inventory_container:
		push_error("背包容器节点未找到")
		return
	
	# 清空背包容器
	for child in inventory_container.get_children():
		child.queue_free()
	
	# 检查玩家引用
	if not player:
		return
	
	# 创建背包网格
	var grid_columns = 6 # 修改为6列以适应更多物品
	var item_size = Vector2(80, 80) # 稍微减小物品尺寸以适应更多物品
	
	# 创建一个字典来存储合并后的物品
	var merged_items = {}
	
	# 遍历背包物品并合并相同物品
	for i in range(player.inventory.size()):
		var item = player.inventory[i]
		var item_key = item.item_name + "_" + str(item.item_type)
		
		if item_key in merged_items:
			# 只有当物品可以堆叠时才合并
			if item.max_stack > 1:
				merged_items[item_key].stack_size += item.stack_size
				merged_items[item_key].inventory_indices.append(i)
			else:
				# 对于不可堆叠的物品，创建新的键以区分
				var unique_key = item_key + "_" + str(i)
				merged_items[unique_key] = {
					"item": item,
					"stack_size": item.stack_size,
					"inventory_indices": [i]
				}
		else:
			merged_items[item_key] = {
				"item": item,
				"stack_size": item.stack_size,
				"inventory_indices": [i]
			}
	
	# 遍历合并后的物品并显示
	for item_data in merged_items.values():
		var item = item_data.item
		
		# 创建物品面板
		var item_panel = Panel.new()
		item_panel.custom_minimum_size = item_size
		
		# 设置面板样式
		var panel_style = StyleBoxFlat.new()
		panel_style.bg_color = Color(0.15, 0.15, 0.15, 0.9)
		panel_style.border_width_left = 2
		panel_style.border_width_top = 2
		panel_style.border_width_right = 2
		panel_style.border_width_bottom = 2
		panel_style.border_color = item.get_quality_color()
		item_panel.add_theme_stylebox_override("panel", panel_style)
		
		# 创建垂直布局
		var vbox = VBoxContainer.new()
		vbox.size_flags_horizontal = Control.SIZE_EXPAND_FILL
		vbox.size_flags_vertical = Control.SIZE_EXPAND_FILL
		vbox.custom_minimum_size = Vector2(70, 70)
		item_panel.add_child(vbox)
		
		# 创建物品名称标签
		var name_label = Label.new()
		name_label.text = item.item_name
		name_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
		name_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
		name_label.add_theme_font_size_override("font_size", 10) # 减小字体大小
		vbox.add_child(name_label)
		
		# 创建物品类型标签
		var type_label = Label.new()
		type_label.text = item.get_type_name()
		type_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
		type_label.add_theme_font_size_override("font_size", 8)
		vbox.add_child(type_label)
		
		# 显示堆叠数量
		if item.max_stack > 1 and item_data.stack_size > 1:
			var count_label = Label.new()
			count_label.text = "x" + str(item_data.stack_size)
			count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
			count_label.vertical_alignment = VERTICAL_ALIGNMENT_BOTTOM
			count_label.size_flags_vertical = Control.SIZE_EXPAND_FILL
			vbox.add_child(count_label)
		
		# 创建物品按钮（透明，覆盖整个面板）
		var item_button = Button.new()
		item_button.flat = true
		item_button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
		item_button.size_flags_vertical = Control.SIZE_EXPAND_FILL
		item_button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
		
		# 连接按钮信号，使用第一个物品的索引
		item_button.pressed.connect(_on_inventory_item_pressed.bind(item_data.inventory_indices[0]))
		item_panel.add_child(item_button)
		
		# 添加到容器
		inventory_container.add_child(item_panel)
	
	# 添加空位占位符，确保总共显示60个格子
	for i in range(player.inventory.size(), 60):
		# 创建空位面板
		var empty_panel = Panel.new()
		empty_panel.custom_minimum_size = item_size
		
		# 设置面板样式
		var panel_style = StyleBoxFlat.new()
		panel_style.bg_color = Color(0.1, 0.1, 0.1, 0.5)
		panel_style.border_width_left = 1
		panel_style.border_width_top = 1
		panel_style.border_width_right = 1
		panel_style.border_width_bottom = 1
		panel_style.border_color = Color(0.3, 0.3, 0.3, 0.5)
		empty_panel.add_theme_stylebox_override("panel", panel_style)
		
		# 创建空位标签
		var empty_label = Label.new()
		empty_label.text = "空"
		empty_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
		empty_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
		empty_label.add_theme_color_override("font_color", Color(0.5, 0.5, 0.5, 0.5))
		empty_panel.add_child(empty_label)
		
		# 添加到容器
		inventory_container.add_child(empty_panel)

# 更新物品信息
# 更新物品信息
func update_item_info() -> void:
	# 检查节点引用
	if not item_info_name or not item_info_type or not item_info_description or not item_info_stats:
		push_error("物品信息节点未找到")
		return
	
	var item = null
	var is_equipped = false
	var equip_slot = -1
	
	# 获取选中的物品
	if player and selected_item_index >= 0 and selected_item_index < player.inventory.size():
		item = player.inventory[selected_item_index]
	
	# 检查是否是装备槽位的物品
	for slot in player.equipment:
		if player.equipment[slot] != null and player.equipment[slot] == item:
			is_equipped = true
			equip_slot = slot
			break
	
	# 更新物品信息显示
	if item:
		# 设置物品名称，使用物品品质颜色
		item_info_name.text = item.item_name
		item_info_name.add_theme_color_override("font_color", item.get_quality_color())
		
		# 设置物品类型
		item_info_type.text = "类型: " + item.get_type_name()
		
		# 设置物品描述
		item_info_description.text = item.description
		
		# 更新物品属性
		for child in item_info_stats.get_children():
			child.queue_free()
		
		# 添加物品属性
		if item.can_equip:
			# 装备槽位
			var slot_label = Label.new()
			slot_label.text = "装备位置: " + item.get_equip_slot_name()
			item_info_stats.add_child(slot_label)
			
			# 等级要求
			var level_label = Label.new()
			level_label.text = "等级要求: " + str(item.required_level)
			item_info_stats.add_child(level_label)
			
			# 属性加成
			var has_bonus = false
			
			if item.health_bonus != 0:
				has_bonus = true
				var health_label = Label.new()
				health_label.text = "生命值: %+d" % item.health_bonus
				if item.health_bonus > 0:
					health_label.add_theme_color_override("font_color", Color(0.2, 0.8, 0.2))
				else:
					health_label.add_theme_color_override("font_color", Color(0.8, 0.2, 0.2))
				item_info_stats.add_child(health_label)
			
			if item.spirit_bonus != 0:
				has_bonus = true
				var spirit_label = Label.new()
				spirit_label.text = "真气值: %+d" % item.spirit_bonus
				if item.spirit_bonus > 0:
					spirit_label.add_theme_color_override("font_color", Color(0.2, 0.8, 0.2))
				else:
					spirit_label.add_theme_color_override("font_color", Color(0.8, 0.2, 0.2))
				item_info_stats.add_child(spirit_label)
			
			if item.attack_bonus != 0:
				has_bonus = true
				var attack_label = Label.new()
				attack_label.text = "攻击力: %+d" % item.attack_bonus
				if item.attack_bonus > 0:
					attack_label.add_theme_color_override("font_color", Color(0.2, 0.8, 0.2))
				else:
					attack_label.add_theme_color_override("font_color", Color(0.8, 0.2, 0.2))
				item_info_stats.add_child(attack_label)
			
			if item.defense_bonus != 0:
				has_bonus = true
				var defense_label = Label.new()
				defense_label.text = "防御力: %+d" % item.defense_bonus
				if item.defense_bonus > 0:
					defense_label.add_theme_color_override("font_color", Color(0.2, 0.8, 0.2))
				else:
					defense_label.add_theme_color_override("font_color", Color(0.8, 0.2, 0.2))
				item_info_stats.add_child(defense_label)
			
			if item.speed_bonus != 0:
				has_bonus = true
				var speed_label = Label.new()
				speed_label.text = "速度: %+d" % item.speed_bonus
				if item.speed_bonus > 0:
					speed_label.add_theme_color_override("font_color", Color(0.2, 0.8, 0.2))
				else:
					speed_label.add_theme_color_override("font_color", Color(0.8, 0.2, 0.2))
				item_info_stats.add_child(speed_label)
			
			if not has_bonus:
				var no_bonus_label = Label.new()
				no_bonus_label.text = "无属性加成"
				item_info_stats.add_child(no_bonus_label)
		elif item.item_type == Item.ItemType.CONSUMABLE:
			# 消耗品效果
			if item.health_restore > 0:
				var health_label = Label.new()
				health_label.text = "恢复生命值: " + str(item.health_restore)
				health_label.add_theme_color_override("font_color", Color(0.2, 0.8, 0.2))
				item_info_stats.add_child(health_label)
			
			if item.spirit_restore > 0:
				var spirit_label = Label.new()
				spirit_label.text = "恢复真气值: " + str(item.spirit_restore)
				spirit_label.add_theme_color_override("font_color", Color(0.2, 0.8, 0.2))
				item_info_stats.add_child(spirit_label)
		
		# 更新按钮状态
		equip_button.visible = item.can_equip and not is_equipped
		equip_button.disabled = false
		unequip_button.visible = is_equipped
		unequip_button.disabled = false
		
		# 更新使用和丢弃按钮状态
		if use_button:
			use_button.visible = item.item_type == Item.ItemType.CONSUMABLE
			use_button.disabled = false
		
		if drop_button:
			drop_button.visible = not is_equipped
			drop_button.disabled = false
	else:
		# 没有选中物品时显示默认信息
		item_info_name.text = "未选择物品"
		item_info_name.add_theme_color_override("font_color", Color(0.8, 0.8, 0.8))
		item_info_type.text = "类型: 无"
		item_info_description.text = "请选择一个物品查看详细信息。"
		
		# 清空属性显示
		for child in item_info_stats.get_children():
			child.queue_free()
		
		# 禁用按钮
		equip_button.visible = false
		unequip_button.visible = false
		if use_button:
			use_button.visible = false
		if drop_button:
			drop_button.visible = false

# 更新玩家属性
func update_player_stats() -> void:
	# 检查节点引用
	if not player_stats_container:
		push_warning("玩家属性容器节点未找到，跳过更新玩家属性")
		return
	
	# 清空属性显示
	for child in player_stats_container.get_children():
		child.queue_free()
	
	# 添加玩家属性
	var health_label = Label.new()
	health_label.text = "生命值: %d/%d" % [player.current_health, player.max_health]
	health_label.add_theme_color_override("font_color", Color(0.8, 0.2, 0.2))
	player_stats_container.add_child(health_label)
	
	var spirit_label = Label.new()
	spirit_label.text = "真气值: %d/%d" % [player.current_spirit, player.max_spirit]
	spirit_label.add_theme_color_override("font_color", Color(0.2, 0.2, 0.8))
	player_stats_container.add_child(spirit_label)
	
	var attack_label = Label.new()
	attack_label.text = "攻击力: %d" % player.attack
	attack_label.add_theme_color_override("font_color", Color(0.8, 0.5, 0.2))
	player_stats_container.add_child(attack_label)
	
	var defense_label = Label.new()
	defense_label.text = "防御力: %d" % player.defense
	player_stats_container.add_child(defense_label)
	
	var speed_label = Label.new()
	speed_label.text = "速度: %d" % player.speed
	player_stats_container.add_child(speed_label)

# 背包物品点击事件
func _on_inventory_item_pressed(item_index: int) -> void:
	# 更新选中的物品索引
	selected_item_index = item_index
	
	# 更新所有物品的选中状态
	for i in range(inventory_container.get_child_count()):
		var item_panel = inventory_container.get_child(i)
		if item_panel is Panel:
			var style = item_panel.get_theme_stylebox("panel").duplicate()
			if i < player.inventory.size():
				var current_item = player.inventory[i]
				if i == selected_item_index:
					# 增加选中物品的边框宽度
					style.border_width_left = 4
					style.border_width_top = 4
					style.border_width_right = 4
					style.border_width_bottom = 4
					# 设置选中状态的边框颜色
					style.border_color = current_item.get_quality_color() * 1.5
				else:
					# 恢复未选中状态的边框
					style.border_width_left = 2
					style.border_width_top = 2
					style.border_width_right = 2
					style.border_width_bottom = 2
					style.border_color = current_item.get_quality_color()
			item_panel.add_theme_stylebox_override("panel", style)
	
	# 更新物品信息显示
	update_item_info()

# 装备槽位点击事件
func _on_equipment_slot_pressed(slot_index: int) -> void:
	# 检查节点引用和玩家引用
	if not player or not item_info_name or not item_info_type or not item_info_description or not item_info_stats:
		push_error("节点引用未找到")
		return
	
	# 显示装备信息
	if player.equipment.has(slot_index) and player.equipment[slot_index] != null:
		var item = player.equipment[slot_index]
		
		# 更新物品信息
		item_info_name.text = item.item_name
		item_info_type.text = "类型: " + item.get_type_name()
		item_info_description.text = item.description
		
		# 更新物品属性
		for child in item_info_stats.get_children():
			child.queue_free()
		
		# 添加物品属性
		# 装备槽位
		var slot_label = Label.new()
		slot_label.text = "装备位置: " + item.get_equip_slot_name()
		item_info_stats.add_child(slot_label)
		
		# 属性加成
		if item.health_bonus != 0:
			var health_label = Label.new()
			health_label.text = "生命值: %+d" % item.health_bonus
			item_info_stats.add_child(health_label)
		
		if item.spirit_bonus != 0:
			var spirit_label = Label.new()
			spirit_label.text = "真气值: %+d" % item.spirit_bonus
			item_info_stats.add_child(spirit_label)
		
		if item.attack_bonus != 0:
			var attack_label = Label.new()
			attack_label.text = "攻击力: %+d" % item.attack_bonus
			item_info_stats.add_child(attack_label)
		
		if item.defense_bonus != 0:
			var defense_label = Label.new()
			defense_label.text = "防御力: %+d" % item.defense_bonus
			item_info_stats.add_child(defense_label)
		
		if item.speed_bonus != 0:
			var speed_label = Label.new()
			speed_label.text = "速度: %+d" % item.speed_bonus
			item_info_stats.add_child(speed_label)
		
		# 更新按钮状态
		if equip_button:
			equip_button.visible = false
		if unequip_button:
			unequip_button.visible = true
			unequip_button.disabled = false
		if use_button:
			use_button.visible = false
		if drop_button:
			drop_button.visible = false
		
		# 设置卸下装备按钮的槽位
		if unequip_button:
			# 断开之前的连接，避免重复连接
			if unequip_button.pressed.is_connected(_on_unequip_button_pressed):
				unequip_button.pressed.disconnect(_on_unequip_button_pressed)
			unequip_button.pressed.connect(_on_unequip_button_pressed.bind(slot_index))

# 装备按钮点击事件
func _on_equip_button_pressed() -> void:
	# 检查玩家引用
	if not player:
		push_error("玩家引用未设置")
		return
		
	if selected_item_index >= 0 and selected_item_index < player.inventory.size():
		# 装备物品
		player.equip_item(selected_item_index)
		
		# 更新界面
		selected_item_index = -1
		update_ui()

# 卸下装备按钮点击事件
func _on_unequip_button_pressed(slot_index: int) -> void:
	# 检查玩家引用
	if not player:
		push_error("玩家引用未设置")
		return
		
	# 卸下装备
	player.unequip_item(slot_index)
	
	# 更新界面
	update_ui()

# 使用物品按钮点击事件
func _on_use_button_pressed() -> void:
	# 检查玩家引用
	if not player:
		push_error("玩家引用未设置")
		return
		
	if selected_item_index >= 0 and selected_item_index < player.inventory.size():
		# 使用物品
		player.use_item(selected_item_index)
		
		# 更新界面
		if selected_item_index >= player.inventory.size():
			selected_item_index = -1
		update_ui()

# 丢弃物品按钮点击事件
func _on_drop_button_pressed() -> void:
	# 检查玩家引用
	if not player:
		push_error("玩家引用未设置")
		return
		
	if selected_item_index >= 0 and selected_item_index < player.inventory.size():
		# 丢弃物品
		player.remove_item(selected_item_index)
		
		# 更新界面
		selected_item_index = -1
		update_ui()

# 关闭按钮点击事件
func _on_close_button_pressed() -> void:
	hide_ui()
